﻿using GuardMonitor.Utils;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using 新门禁.Utils;

namespace 新门禁
{
    /// <summary>
    /// PassWindow.xaml 的交互逻辑
    /// </summary>
    public partial class PassWindow : Window
    {
        string inOutType = "";
        string carNo = "";
        public PassWindow()
        {
            InitializeComponent();
        }

        public void setInOutType(String type)
        {
            this.inOutType = type;
        }
        public void setCarNo(String carNo)
        {
            this.carNo = carNo;
         }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            

            if (txtMemo.Text.Trim().Length < 2)
            {
                MessageBox.Show("请输入手动放行原因");
                return;
            }
            //服务器验证是否通行
            try
            {
                String gateCode = App.GetBaseInfo().gateCode;
                String[] gatecodes = gateCode.Split(",".ToCharArray(), StringSplitOptions.RemoveEmptyEntries);
                String ip = App.GetBaseInfo().HttpServerIp;
                String port = App.GetBaseInfo().HttpServerPort;
                JArray jArray = new JArray();
                for (int i = 0; i < gatecodes.Length; i++)
                {
                    
                    JObject reqJSON = new JObject();
                    reqJSON.Add("gateCode", gatecodes[i]);
                    reqJSON.Add("carNo", carNo);
                    reqJSON.Add("type", inOutType);
                    reqJSON.Add("manual", "1");
                    reqJSON.Add("memo", txtMemo.Text.Trim());
                    reqJSON.Add("userid", App.GetGlobalVariablesModel().LoginUserID);
                    reqJSON.Add("usercode", App.GetGlobalVariablesModel().LoginUserCode);
                    reqJSON.Add("username", App.GetGlobalVariablesModel().LoginUserName);
                    jArray.Add(reqJSON);
                }
                //String bodyJsonStr = "{\"gateCode\":\"" + gateCode + "\", \"carNo\":\"" + carno + "\", \"type\":\"" + inOutType + "\"}";
                JObject newJson = new JObject();
                newJson.Add("jsonList", jArray);

                String result = HttpPostHelper.doHttpPost("http://" + ip + ":" + port + "/qd-s-factory/access/carApproachs", newJson.ToString());
                JObject resJSON = JObject.Parse(result);
                string code = (string)resJSON.GetValue("code");
                string message = (string)resJSON.GetValue("message");

                if ("00000".Equals(code))
                {
                    TcpRequestUtil.Main(inOutType);
                    //TcpRequestUtil.Main("in");
                    //TcpRequestUtil.Main("out");
                    //String comm = App.GetBaseInfo().IO_Com_PortName;
                    ////抬杆
                    //if ("in".Equals(inOutType))
                    //{
                    //    //控制入口门禁设备打开

                    //    String inOpenPort = App.GetBaseInfo().IO_In_Open_Port;
                    //    PortControlHelper.openDoor(comm, int.Parse(inOpenPort));
                    //}
                    //else if ("out".Equals(inOutType))
                    //{
                    //    //控制出口门禁设备打开。
                    //    String outOpenPort = App.GetBaseInfo().IO_Out_Open_Port;
                    //    PortControlHelper.openDoor(comm, int.Parse(outOpenPort));
                    //}

                    this.DialogResult = true;
                    this.Close();
                }
                else
                {
                    MessageBox.Show(message);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }
    }
}
